package com.graduation.lastproject.controller.home.homeHighActive;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.graduation.lastproject.entity.*;
import com.graduation.lastproject.service.admin.AdminHighActive.AdminHighService;
import com.graduation.lastproject.service.home.HomeHighActice.HomeHighService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/homeHigh")
public class HomeHighController {

    @Autowired
    private HomeHighService homeHighService;

    //获取当前用户所有订单
    @RequestMapping(value = "/mybookorder")
    public Object orderlist(HttpServletRequest request,@RequestParam(value ="pageNum", defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize, Model model){
        /*当前用户电话 主键*/
        String myPhone=((User)request.getSession().getAttribute("userInfo")).getPhone();
        PageHelper.startPage(pageNum,pageSize);
        //查询当前用户的订单
        List<Bookorder> list=homeHighService.getAllBookorder(myPhone);
        //将数据存入到pageInfo中
        PageInfo<Bookorder> pageInfo=new PageInfo<>(list);
        /*model 存值*/
        model.addAttribute("pageInfo",pageInfo);
        return "home/mybookorder";
    }

    /*删除用户某一个订单*/
    @RequestMapping(value = "/deleteOrderById" ,method = RequestMethod.POST)
    @ResponseBody
    public Object deleteOrder(String id){
        Map<String,Object> result=new HashMap<>();
        int i=homeHighService.deleteOrderById(id);
        result.put("msg",i==1 ? "删除成功":"删除失败");
        return result;
    }

    /*用户发表评论*/
    @RequestMapping(value = "/review",method = RequestMethod.POST)
    @ResponseBody
    public Object review(HttpServletRequest request){
        Map<String,Object> result=new HashMap<>();
        Map<String,Object> map=parameters(request);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd") ;
        map.put("reviewTime",simpleDateFormat.format(new Date()));
        int i=homeHighService.insertReview(map);
        result.put("msg",i==1 ? "评论成功" : "评论失败");
        return  result;
    }

    /*获取当前用户的所有评论*/
    @RequestMapping(value = "/myReview")
    public Object reviewList(HttpServletRequest request,
                             @RequestParam(value = "pageNum" ,defaultValue = "1") int pageNum,
                             @RequestParam(defaultValue = "5")int pageSize,
                             Model model){
         /*当前用户电话 主键*/
        String myPhone=((User)request.getSession().getAttribute("userInfo")).getPhone();
        PageHelper.startPage(pageNum,pageSize);
        List<Review> list=homeHighService.getAllReview(myPhone);
        PageInfo<Review> pageInfo=new PageInfo<>(list);
        model.addAttribute(pageInfo);
        return "home/myreview";
    }
    /*用户删除掉某一个评论*/
    @RequestMapping(value = "/deleteReviewById" , method = RequestMethod.POST)
    @ResponseBody
    public Object deleteReview(HttpServletRequest request){
        Map<String,Object> result=new HashMap<>() ;
        //评论表主键id
        String id=request.getParameter("id") ;
        int i=homeHighService.deleteReviewById(id);
        result.put("msg",i==1 ? "删除成功" : "删除失败");
        return result;
     }

     /*追加评论*/
     @RequestMapping(value = "/reviewAdd" ,method = RequestMethod.POST)
     @ResponseBody
     public Object reviewAdd(HttpServletRequest request){
         Map<String,Object> result=new HashMap<>() ;
         Map<String,Object> map=parameters(request);
         int i= homeHighService.reviewAdd(map);
         result.put("msg",i==1 ? "追评成功" :"追评失败");
         return  result;
     }

     /*获取所有积分可兑换商品*/
     @RequestMapping(value = "/myintegral")
     public Object integralList(HttpServletRequest request,
                                @RequestParam(defaultValue = "1" ,value = "pageNum")int pageNum,
                                @RequestParam(defaultValue = "4")int pageSize,
                                Model model){
         //获取用户最新数据
         /*获取当前登录人主键Id*/
         String id=((User)request.getSession().getAttribute("userInfo")).getId();
         User user=homeHighService.getUserInfo(id);
         request.setAttribute("userIntegral",user.getIntegral());
         /*根据vip等级展示不同数量的商品*/
         /*int state=((User)request.getSession().getAttribute("userInfo")).getVip();*/
         int state=Integer.parseInt(user.getVip().toString());
         PageHelper.startPage(pageNum,pageSize);
         /*获取所有商品*/
         List<Commodity> list=homeHighService.getCommodityList(state);
         PageInfo pageInfo=new PageInfo(list);
         model.addAttribute(pageInfo);
         return  "home/mycommodity";
    }

    /*兑换商品*/
    @RequestMapping(value = "/integralEx",method = RequestMethod.POST)
    @ResponseBody
    public Object addintegralEx(HttpServletRequest request){
        Map<String,Object> result=new HashMap<>();
         /*怕到时候忘记操作 不在service层添加当前用户id*/
        String userId=((User)request.getSession().getAttribute("userInfo")).getId();
        Map<String,Object> map = parameters(request);
        map.put("userId",userId);
        /*新建商品订单  触发器更新商品库存、用户积分*/
        int i=homeHighService.insertExchangeOrder(map);
        result.put("msg",i==1 ? "兑换成功" : "兑换失败");
        return  result;
    }

    /*获取我的所有积分兑换记录*/
    @RequestMapping(value = "/myComtyoOrderList")
    public Object mycomtyorderList(HttpServletRequest request,
                                   @RequestParam(defaultValue = "1" ,value = "pageNum")int pageNum,
                                   @RequestParam(defaultValue = "5")int pageSize,
                                   Model model){
        /*获取当前登录人主键Id*/
        String id=((User)request.getSession().getAttribute("userInfo")).getId();
        //获取用户最新数据
        User user=homeHighService.getUserInfo(id);
        request.setAttribute("userVip",user.getVip());
        request.setAttribute("userIntegral",user.getIntegral());
        PageHelper.startPage(pageNum,pageSize);
        /*获取当前登录人的所有商品记录*/
        List<Commodityorder> list=homeHighService.getAllComorder(id);
        PageInfo<Commodityorder> pageInfo=new PageInfo<>(list);
        model.addAttribute(pageInfo);
        return "home/mycomtyorder";
    }

    /*删除积分兑换记录*/
    @RequestMapping(value = "/deleteMycommodityOrderById" ,method = RequestMethod.POST)
    @ResponseBody
    public Object deleteMycommodityOrderById(String id){
        Map<String,Object> result=new HashMap<>();
        int i=homeHighService.deleteMycommodityOrderById(id);
        result.put("msg",i==1 ? "删除成功":"删除失败");
        return result;
    }

    /*确认收货*/
    @RequestMapping(value = "/getInMycommodityOrderById" ,method = RequestMethod.POST)
    @ResponseBody
    public Object getInMycommodityOrderById(String id){
        Map<String,Object> result=new HashMap<>();
        int i=homeHighService.getInMycommodityOrderById(id);
        result.put("msg",i==1 ? "收货成功":"收货失败");
        return result;
    }

    /*request装map*/
    public final Map<String,Object> parameters(HttpServletRequest request){
        Map<String,Object> param=new HashMap<>();
        if(request!=null){
            Enumeration<String> keys=request.getParameterNames();
            while (keys.hasMoreElements()) {
                String key=keys.nextElement();
                try {
                    String value=request.getParameter(key);
                    param.put(key,value);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return param;
    }
}
